package dev.caobaoqi6040.backend.infrastructure.web;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

/**
 * JacksonConfiguration
 *
 * @author caobaoqi6040
 * @since 2025/10/9 17:43
 */
@Configuration
public class JacksonConfiguration {

	@Bean
	public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
		return (Jackson2ObjectMapperBuilder builder) -> {
			// === Core serialization behavior ===
			builder.serializationInclusion(JsonInclude.Include.NON_NULL); // Ignore nulls
			builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); // Don’t use timestamps
			builder.featuresToDisable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); // Ignore unknown fields
			builder.modules(new JavaTimeModule()); // Support LocalDateTime, etc.

			// === Global time format ===
			builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");

			// === Naming strategy ===
			builder.propertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE);

			// === Optional: for better logs in dev ===
			// builder.featuresToEnable(SerializationFeature.INDENT_OUTPUT);
		};
	}

}
